package com.itheima.Service.ServiceImpl;

import com.itheima.Dao.SetmealMapper;
import com.itheima.Service.SetmealService;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<Setmeal> getSetmeal(){
        return setmealMapper.getSetmeal();
    }

    @Override
    @Transactional
    public Setmeal findById(int id){

//        Setmeal setmeal = setmealMapper.findBySetmealId(id);
//        List<CheckGroup> checkGroups = new ArrayList<>();
//        List<Integer> checkGroupIds = setmealMapper.findCheckGroupIdsBySetmealId(id);
//        for (Integer checkGroupId : checkGroupIds) {
//            log.info("根据套餐id查询关联的检查组id:{}",checkGroupId);
//            CheckGroup checkGroup = setmealMapper.findCheckGroupById(checkGroupId);
//
//
//            List<Integer> checkItemIds = setmealMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
//            List<CheckItem> checkItems = new ArrayList<>();
//            for (Integer checkItemId : checkItemIds) {
//                log.info("根据检查组id查询关联的检查项id：{}",checkItemId);
//                checkItems.add(setmealMapper.findCheckItemById(checkItemId));
//            }
//
//            checkGroup.setCheckItems(checkItems);
//
//
//            checkGroups.add(checkGroup);
//        }
//
//
//        setmeal.setCheckGroups(checkGroups);


        //TODO: 2025/3/12 优化代码(多表操作优化)
        Setmeal setmeal = setmealMapper.findBySetmealId(id);
        List<Integer> checkGroupIds = setmealMapper.findCheckGroupIdsBySetmealId(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        for (Integer checkGroupId : checkGroupIds) {
            List<CheckGroup> list = setmealMapper.findById(checkGroupId);
            checkGroups.addAll(list);
        }
        setmeal.setCheckGroups(checkGroups);


        return setmeal;

    }
}
